package com.iflytek.hive.need3;


import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.*;

public class AmountDiffReducer extends Reducer<Text, Text, Text, Text> {
    private final Text valueOut = new Text();
    int i = 0;
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {
        List<String> list = new LinkedList<>();;
        int index = 0;

        String out ="";
        for (Text value: values) {
            //System.out.println(index);
            list.add(value.toString());
            System.out.println(index+list.toString());
            if (index!=0){
                Collections.sort(list);
                double num1 =  Double.valueOf(list.get(1).substring(5));
                double num0 =  Double.valueOf(list.get(0).substring(5));
                double diff = num1 - num0;
                out = list.get(1).substring(5)+","+list.get(0).substring(5)+","+String.valueOf(diff);
                valueOut.set(out);
                context.write(key, valueOut);
            }else if (index == 0 & i == 0) {
                out = list.get(0).substring(5)+",0,"+list.get(0).substring(5);
                valueOut.set(out);
                context.write(key, valueOut);
                i++;
            }
            index++;
        }

    }
}
